package com.xq.shop.service.admin.impl;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xq.shop.dao.IBaseDAO;
import com.xq.shop.dao.IMenuDAO;
import com.xq.shop.po.Menu;
import com.xq.shop.service.admin.IMenuService;

@Service("admin_menu_service")
public class MenuService extends BaseService<Menu> implements IMenuService {

	@Autowired
	private IMenuDAO menuDAO;

	@Override
	public IBaseDAO<Menu> getDAO() {
		return menuDAO;
	}
	
	@Override
	public List<Menu> getByPID(Long id) {
		return menuDAO.getByPID(id);
	}

	@Override
	public List<Menu> getMenu2nd() {
		
		List<Menu> allMenu_List = menuDAO.getAll();
		
		List<Menu> result = new ArrayList<Menu>();
		if(allMenu_List != null && allMenu_List.size() > 1){
			
			for(Menu menu : allMenu_List){
				
				if(menu.getParent_id() != 0) result.add(menu);
			}
		}
		
		return result;
	}

	@Override
	public Map<Menu, List<Menu>> getMenuTree(Long pID) {
		
		List<Menu> allMenus = menuDAO.getAll();
		Map<Menu, List<Menu>> result = new LinkedHashMap<Menu, List<Menu>>();
		if(allMenus != null && allMenus.size() > 0){
			
			for(Menu menu : allMenus){
				
				if(!pID.equals(menu.getParent_id())) continue;
				
				List<Menu> child = new ArrayList<Menu>();
				for(Menu tmp : allMenus){
					
					if(menu.getId().equals(tmp.getParent_id())) child.add(tmp);
				}
				result.put(menu, child);
			}
		}
		return result;
	}
}
